package com.weijianhuawen.wjblog.common.utils;

import com.weijianhuawen.wjblog.common.enums.ResponseCodeEnum;
import com.weijianhuawen.wjblog.common.exception.UserException;
import lombok.Data;

import java.io.Serializable;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-10-15 11:45
 */
@Data
public class Response<T> implements Serializable {
    //响应状态
    private Boolean success = true;
    //响应状态码
    private String code;
    //响应数据
    private T data;
    //响应信息
    private String message;

    //响应成功
    public static<T> Response<T> success() {
        Response<T> resp = new Response<>();
        resp.setCode(ResponseCodeEnum.SUCCESS.getErrorCode());
        resp.setMessage(ResponseCodeEnum.SUCCESS.getErrorMessage());
        return resp;
    }
    public static<T> Response<T> success(T data) {
        Response<T> resp = new Response<>();
        resp.setCode(ResponseCodeEnum.SUCCESS.getErrorCode());
        resp.setMessage(ResponseCodeEnum.SUCCESS.getErrorMessage());
        resp.setData(data);
        return resp;
    }

    //失败响应
    public static<T> Response<T> fail() {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage("未知错误！");
        return response;
    }

    public static <T> Response<T> fail(ResponseCodeEnum responseCodeEnum) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(responseCodeEnum.getErrorMessage());
        response.setCode(responseCodeEnum.getErrorCode());
        return response;
    }
    public static<T> Response<T> fail(String errMessage) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(errMessage);
        return response;
    }

    public static<T> Response<T> fail(String errCode, String errMessage) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(errMessage);
        response.setCode(errCode);
        return response;
    }

    public static <T> Response<T> fail(UserException userException) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(userException.getErrorMessage());
        response.setCode(userException.getErrorCode());
        return response;
    }
}
